package com.csx.saas.platform.init.controller;

import com.csx.basic.annotation.log.SysLog;
import com.csx.basic.base.R;
import com.csx.basic.base.controller.SuperCacheController;
import com.csx.basic.database.mybatis.conditions.Wraps;
import com.csx.basic.utils.TreeUtil;
import com.csx.saas.platform.init.dto.InitProductCatePageQuery;
import com.csx.saas.platform.init.dto.InitProductCateSaveDTO;
import com.csx.saas.platform.init.dto.InitProductCateUpdateDTO;
import com.csx.saas.platform.init.entity.InitProductCate;
import com.csx.saas.platform.init.service.InitProductCateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 产品分类
 * </p>
 *
 * @author ChenSongXin
 * @date 2021-03-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/initProductCate")
@Api(value = "InitProductCate", tags = "产品分类")
//@PreAuth(replace = "platform:initProductCate:")
public class InitProductCateController extends SuperCacheController<InitProductCateService, Long, InitProductCate, InitProductCatePageQuery, InitProductCateSaveDTO, InitProductCateUpdateDTO> {

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<InitProductCate> initProductCateList = list.stream().map((map) -> {
            InitProductCate initProductCate = InitProductCate.builder().build();
            //TODO 请在这里完成转换
            return initProductCate;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(initProductCateList));
    }

    @ApiOperation(value = "查询系统所有的分类树", notes = "查询系统所有的分类树")
    @GetMapping("/tree")
    @SysLog("查询系统所有的分类树")
    public R<List<InitProductCate>> allTree() {
        List<InitProductCate> list = baseService.list(Wraps.<InitProductCate>lbQ().orderByAsc(InitProductCate::getSortValue));
        return success(TreeUtil.buildTree(list));
    }
}
